package com.qm.hibernate;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * Tfriends entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.qm.hibernate.Tfriends
 * @author MyEclipse Persistence Tools
 */

public class TfriendsDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TfriendsDAO.class);

	public void save(Tfriends transientInstance) {
		log.debug("saving Tfriends instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Tfriends persistentInstance) {
		log.debug("deleting Tfriends instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Tfriends findById(com.qm.hibernate.TfriendsId id) {
		log.debug("getting Tfriends instance with id: " + id);
		try {
			Tfriends instance = (Tfriends) getSession().get(
					"com.qm.hibernate.Tfriends", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Tfriends instance) {
		log.debug("finding Tfriends instance by example");
		try {
			List results = getSession()
					.createCriteria("com.qm.hibernate.Tfriends")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Tfriends instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Tfriends as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Tfriends instances");
		try {
			String queryString = "from Tfriends";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Tfriends merge(Tfriends detachedInstance) {
		log.debug("merging Tfriends instance");
		try {
			Tfriends result = (Tfriends) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Tfriends instance) {
		log.debug("attaching dirty Tfriends instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Tfriends instance) {
		log.debug("attaching clean Tfriends instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	public void addRelation(Integer myId,Integer hisId)
	{
		String sql = "select * from tfriends where userid = ? or friendId = ?";
		Session s = getSession();
		s.beginTransaction();	
		TfriendsDAO fd = new TfriendsDAO();
		
		Tfriends f = new Tfriends();
		TfriendsId fid = new TfriendsId();
		fid.setFriendId(hisId);
		fid.setUserId(myId);
		f.setId(fid);
		fd.save(f);
		s.getTransaction().commit();
		s.close();
		
		Session s2 = getSession();
		s2.beginTransaction();
		fid.setFriendId(myId);
		fid.setUserId(hisId);
		f.setId(fid);
		fd.save(f);
		s2.getTransaction().commit();
		s2.close();
	}
	public void delRelation(Integer myId,Integer hisId)
	{
	
		Session s = getSession();
		s.clear();
		s.beginTransaction();	
		System.out.println("delRelation is called begin");
		Tfriends f = new Tfriends();
		TfriendsId fid = new TfriendsId();
		TfriendsDAO fd = new TfriendsDAO();
		fid.setFriendId(hisId);
		fid.setUserId(myId);
		f.setId(fid);
		fd.delete(f);
		s.getTransaction().commit();
		s.close();
		
		System.out.println("delRelation is called middle");
		
		Tfriends f2 = new Tfriends();
		Session s2 = getSession();
		s2.beginTransaction();	
		fid.setFriendId(myId);
		fid.setUserId(hisId);
		f2.setId(fid);
		fd.delete(f2);
		s2.getTransaction().commit();
		s2.close();
		System.out.println("delRelation is called");
	}

	public boolean isHaveRelation(Integer myId) {
		String sql = "select * from tfriends where userid = ? or friendId = ?";	
		List l = getSession().createSQLQuery(sql).addEntity(Tfriends.class).setInteger(0, myId).setInteger(1, myId).list();

		if (l.size() > 0) return true;
		return false;
	}
}